package com.aclass.xqx.classscreen.ui;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import com.aclass.xqx.classscreen.BaseApplication;
import com.aclass.xqx.classscreen.R;
import com.aclass.xqx.classscreen.adapter.StudentToAdapter;
import com.aclass.xqx.classscreen.adapter.StudentnoToAdapter;
import com.aclass.xqx.classscreen.db.CardDao;
import com.aclass.xqx.classscreen.db.CardStudent;
import com.aclass.xqx.classscreen.db.Cardnumber;
import com.aclass.xqx.classscreen.model.MainModel;
import com.aclass.xqx.classscreen.model.StudentPhotosModel;
import com.aclass.xqx.classscreen.utlis.ClassCalendar;
import com.aclass.xqx.classscreen.utlis.Constant;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by xqx on 2017/11/6.
 * 照片墙
 */

public class StudentPhotosActivity extends AppCompatActivity {
    GridView rl_noto,rl_to;
    private TextView tv_countdown,tv_number,tv_toNum,tv_classtime,tv_day,tv_weekDays,tv_weather,tv_to,tv_Notto,tv_GoingTo;
    private MainModel mainModel;
    StudentPhotosModel model;

    int i=60;
    Timer timer;
    private EditText tbCard;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_studentphotos);
        BaseApplication.getDaoInstant().clear();
        initView();
        initData();
        initIdNumber();
    }

    private void initView() {
        mainModel=new MainModel(this);
        model=new StudentPhotosModel();
        rl_to= (GridView) findViewById(R.id.rl_to);
        rl_noto= (GridView) findViewById(R.id.rl_notto);
        tv_countdown= (TextView) findViewById(R.id.tv_countdown);
        tv_number= (TextView) findViewById(R.id.tv_number);
        tv_GoingTo= (TextView) findViewById(R.id.tv_GoingTo);
        tbCard= (EditText) findViewById(R.id.tbCard);
        tv_number.setText(mainModel.getNumber()+" 人");
        tv_toNum= (TextView) findViewById(R.id.tv_toNum);
        tv_toNum.setText(mainModel.getTonum()+" 人");
        tv_to= (TextView) findViewById(R.id.tv_to);
        tv_to.setText("实到"+mainModel.getTonum()+" 人");
        tv_Notto= (TextView) findViewById(R.id.tv_Notto);
        tv_Notto.setText("未到"+(mainModel.getNumber()-mainModel.getTonum())+" 人");
        tv_day= (TextView) findViewById(R.id.tv_day);
        tv_day.setText(ClassCalendar.getStringDay());
        tv_classtime= (TextView) findViewById(R.id.tv_classtime);
        tv_classtime.setText(ClassCalendar.getInttimes());
        tv_weekDays= (TextView) findViewById(R.id.tv_weekDays);
        tv_weekDays.setText(ClassCalendar.getStringWeekDays());
        tv_weather= (TextView) findViewById(R.id.tv_weather);
        tv_GoingTo.setText(model.GoingTo());
        String weather = getIntent().getStringExtra(Constant.WEATHER);
        tv_weather.setText(weather);
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv_classtime.setText(ClassCalendar.getInttimes());
                    }
                });

            }
        },1000,15000);
    }
    public static StudentToAdapter adapter;
    public static StudentnoToAdapter adapter1;
    private void initData() {
//        GridLayoutManager layoutManager = new GridLayoutManager(this,8);
//        rl_noto.setLayoutManager(layoutManager);


        List<CardStudent> tonum = model.getTonum();
        List<CardStudent> students = model.getonTonum();

        adapter=new StudentToAdapter(this,tonum);
        Log.e("ATG",students.size()+"");
        adapter1=new StudentnoToAdapter(this,students);
        rl_noto.setAdapter(adapter1);

        rl_to.setAdapter(adapter);
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (; i >0; i--) {
                    handler.sendEmptyMessage(1);
                    if(i<=0){
                        break;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                handler.sendEmptyMessage(2);
            }
        }).start();
    }
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == 1) {
                tv_countdown.setText("还剩" + i + "关闭界面");
            } else {
                i = 40;
               finish();

            }
        };
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel( );
            timer = null;
        }
    }
    private void initIdNumber() {
        tbCard.requestFocus();
        tbCard.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                if (actionId== EditorInfo.IME_ACTION_SEND||actionId == EditorInfo.IME_ACTION_DONE||event != null && KeyEvent.KEYCODE_ENTER==event.getKeyCode()&&KeyEvent.ACTION_DOWN==event.getAction()){
                    String strCardNo=tbCard.getText().toString().trim();
                    Log.e("ATG",strCardNo);
                    List<Cardnumber> card = CardDao.queryLove(strCardNo);
                    if (card.size()!=0){
                        tbCard.setText("");
                        finish();
                    }
//                }
                return false;
            }
        });
    }
}
